package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.server.service.MyCenterService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("users")
@Slf4j
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    //根据id查看用户信息
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserByUserId(@RequestParam(value = "userId", required = false) Long userId) {
        try {
            UserInfoVo userInfoVo = this.myCenterService.queryUserInfoByUserId(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据用户id查询用户信息出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //更新用户信息
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        try {
            Boolean bool = this.myCenterService.updateUserInfo(userInfoVo);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //黑名单列表
    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlacklist(@RequestParam("page") Integer page, @RequestParam("pagesize") Integer pageSize) {
        try {
            PageResult pageResult = myCenterService.queryBlacklist(page, pageSize);

            return ResponseEntity.ok(pageResult);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    //黑名单 - 移除
    @DeleteMapping("blacklist/{id}")
    public ResponseEntity<Void> blackListRemove(@PathVariable("id") String id) {
        int result = myCenterService.blackListRemove(id);
        if (result == 1) {
            return ResponseEntity.ok(null);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    //互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("counts")
    public ResponseEntity<Map> counts() {
        Map<String,Integer> map = myCenterService.queryCounts();
        return ResponseEntity.ok(map);
    }

    //喜欢 - 取消
    @DeleteMapping("like/{id}")
    public ResponseEntity<Void> dislike(@PathVariable("id") String id) {
        myCenterService.dislike(id);
        return ResponseEntity.ok(null);
    }

    //粉丝 - 喜欢
    @PostMapping("fans/{id}")
    public ResponseEntity<Void> fansLove(@PathVariable("id") String id) {
        myCenterService.fansLove(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 更改陌生人问题
     *
     * @param map
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity saveQuestion(@RequestBody Map<String, String> map) {
        //获取问题内容
        String content = map.get("content");
        //保存陌生人问题
        myCenterService.saveQuestion(content);
        //返回结果
        return ResponseEntity.ok(null);
    }

    /**
     * 查询用户设置
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity querySettings() {
        SettingsVo settingsVo = myCenterService.querySettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 保存通知设置
     *
     * @param map
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity saveNotifications(@RequestBody Map<String, Boolean> map) {
        myCenterService.saveNotifications(map);
        return ResponseEntity.ok(null);
    }


    /**
     * 修改手机号_发送验证码
     *
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        myCenterService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }


    /**
     * 修改手机号_校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> map) {
        String code = map.get("verificationCode");
        Map<String,Object> resultMap = myCenterService.checkVerificationCode(code);
        return ResponseEntity.ok(resultMap);
    }


    /**
     * 保存新手机号
     *
     * @param map
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity updatePhone(@RequestBody Map<String,String> map) {
        String phone = map.get("phone");
        myCenterService.updatePhone(phone);

        System.out.println("123");
        return ResponseEntity.ok(null);
    }


    /**
     * 查询用户互相喜欢、喜欢、粉丝、谁看过我列表
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     *
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("friends/{type}")
    public ResponseEntity queryUser(@PathVariable("type") String type,
                                    @RequestParam(required = true,value = "page") Integer page,
                                    @RequestParam(value = "pagesize",defaultValue = "10",required = false)Integer pageSize,
                                    @RequestParam(value = "nickname",required = false)String nickName) {
        PageResult pageResult = myCenterService.queryUser(type, page, pageSize,nickName);
        return ResponseEntity.ok(pageResult);
    }


    //是否已喜欢
    @GetMapping("{id}/alreadyLove")
    public ResponseEntity<Boolean> alreadyLove(@PathVariable("id") String id){
        Boolean result = myCenterService.isAlreadyLove(id);
        return ResponseEntity.ok(result);
    }

}
